// rock wang: 用于sort()函数中用到的比较两个字符串现后顺序的函数
/**
 * 比较两个字符串的先后顺序（按照字典序，以unicode码为标准排序）
 * @param {传入的对象的用来比较的属性，如student{"name":rock_wang, "age": 18}中的"name"} propName 
 * 输入参数可以是包含多级属性的字符串，属性之间用空格分开
 */
export function compareByProp(propNameString) {
    return function(a, b) {
        let propNameList = propNameString.trim().split(/\s+/)
        let depth = propNameList.length

        var requiredElementOfA = a
        var requiredElementOfB = b

        for (let i = 0; i < depth; i++) {
            requiredElementOfA = requiredElementOfA[propNameList[i]]
            requiredElementOfB = requiredElementOfB[propNameList[i]]
        }
        if (typeof(requiredElementOfA) === 'number') {
            if (requiredElementOfA > requiredElementOfB) {
                return 1;
            } else if (requiredElementOfA < requiredElementOfB) {
                return -1;
            } else {
                return 0;
            }
        } else {
            let lengthA = requiredElementOfA.length
            let lengthB = requiredElementOfB.length

            let i = 0
            while (i < lengthA && i < lengthB) {
                if (requiredElementOfA.charCodeAt(i) > requiredElementOfB.charCodeAt(i)) {
                    return 1
                } else if (requiredElementOfA.charCodeAt(i) < requiredElementOfB.charCodeAt(i)) {
                    return -1
                } else {
                    i++
                }
            }
            if (i == lengthA) {
                if (i == lengthB) {
                    return 0
                } else {
                    return -1
                }
            } else {
                return 1
            }
        }
    }
}

export function compareByPinyin(propNameString) {
    return function(a, b) {
        let propNameList = propNameString.trim().split(/\s+/)
        let depth = propNameList.length

        var requiredElementOfA = a
        var requiredElementOfB = b

        for (let i = 0; i < depth; i++) {
            requiredElementOfA = requiredElementOfA[propNameList[i]]
            requiredElementOfB = requiredElementOfB[propNameList[i]]
        }
        return requiredElementOfA.localeCompare(requiredElementOfB, "zh")
    }
}

export function compareTimeString(dateA, dateB) {
    let lengthA = dateA.length
    let lengthB = dateB.length
    
    let i = 0
    while (i < lengthA && i < lengthB) {
        if (dateA.charCodeAt(i) > dateB.charCodeAt(i)) {
            return 1
        } else if (dateA.charCodeAt(i) < dateB.charCodeAt(i)) {
            return -1
        } else {
            ++i
        }
    }
    if (i === lengthA) {
        if (i === lengthB) {
            return 0
        } else {
            return -1
        }
    } else {
        return 1
    }
}
export function compareReportType(dateA, dateB) {
    const map = {
        '日报':1,
        '周报':2,
        '月报':3,
        '年报':4,
        '毕业总结':5,
    }
    return map[dateA]-map[dateB]
}